import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { preProcessImageUrl } from '../../src/img/transform';

describe('preapareImageUrl', () => {
  it('url is not a string will throw an error', async () => {
    await expect(preProcessImageUrl(1 as any, false)).rejects.toThrowError(
      'url must be a string, but got 1 with type number',
    );
    await expect(preProcessImageUrl({} as any, false)).rejects.toThrowError(
      'url must be a string, but got [object Object] with type object',
    );
    await expect(preProcessImageUrl(null as any, false)).rejects.toThrow(
      'url must be a string, but got null with type object',
    );
    await expect(preProcessImageUrl(undefined as any, false)).rejects.toThrow(
      'url must be a string, but got undefined with type undefined',
    );
  });

  it('base64 string will not be converted', async () => {
    expect(await preProcessImageUrl('', false)).toBe(
      '',
    );
  });

  it('local file path will be converted to base64', async () => {
    expect(
      await preProcessImageUrl(
        path.resolve(__dirname, '../fixtures/2x2.jpeg'),
        false,
      ),
    ).toMatchInlineSnapshot(
      `""`,
    );
  });

  it('http url will not be converted to base64 by default', async () => {
    expect(
      await preProcessImageUrl(
        'https://github.githubassets.com/favicons/favicon.svg',
        false,
      ),
    ).toBe('https://github.githubassets.com/favicons/favicon.svg');
  });

  it('http url will be converted to base64 if convertHttpImage2Base64 is true', async () => {
    expect(
      await preProcessImageUrl(
        'https://github.githubassets.com/favicons/favicon.svg',
        true,
      ),
    ).toMatchInlineSnapshot(
      `""`,
    );
  });
});
